import { request } from '@/service/request';

/**
 * 数据标签API
 */

/** 标签类型 */
export interface DataTag {
  id?: number;
  tagName: string;
  tagCode: string;
  parentId?: number;
  tagLevel?: number;
  tagType: string;
  description?: string;
  color?: string;
  icon?: string;
  sortOrder?: number;
  autoTagRule?: string;
  status?: number;
  usageCount?: number;
  projectId?: number;
  createTime?: string;
  updateTime?: string;
  remark?: string;
  children?: DataTag[];
  assetCount?: number;
}

/** 标签关联DTO */
export interface TagRelationDTO {
  assetId: number;
  assetType: string;
  tagIds: number[];
}

/** 标签统计DTO */
export interface TagStatisticsDTO {
  tagId: number;
  tagName: string;
  tagType: string;
  usageCount: number;
  assetCount: number;
}

/**
 * 查询标签树
 */
export function fetchTagTree(projectId?: number) {
  return request<DataTag[]>({
    url: '/dataplatform/tag/tree',
    method: 'get',
    params: { projectId }
  });
}

/**
 * 查询标签列表
 */
export function fetchTagList(params: any) {
  return request({
    url: '/dataplatform/tag/list',
    method: 'get',
    params
  });
}

/**
 * 查询标签详情
 */
export function fetchTagDetail(id: number) {
  return request<DataTag>({
    url: `/dataplatform/tag/${id}`,
    method: 'get'
  });
}

/**
 * 创建标签
 */
export function addTag(data: DataTag) {
  return request<number>({
    url: '/dataplatform/tag',
    method: 'post',
    data
  });
}

/**
 * 更新标签
 */
export function updateTag(data: DataTag) {
  return request({
    url: '/dataplatform/tag',
    method: 'put',
    data
  });
}

/**
 * 删除标签
 */
export function deleteTag(ids: number[]) {
  return request({
    url: `/dataplatform/tag/${ids.join(',')}`,
    method: 'delete'
  });
}

/**
 * 为资产添加标签
 */
export function addTagsToAsset(data: TagRelationDTO) {
  return request({
    url: '/dataplatform/tag/relation',
    method: 'post',
    data
  });
}

/**
 * 移除资产标签
 */
export function removeTagsFromAsset(assetId: number, assetType: string, tagIds: number[]) {
  return request({
    url: '/dataplatform/tag/relation',
    method: 'delete',
    params: { assetId, assetType, tagIds: tagIds.join(',') }
  });
}

/**
 * 查询资产的标签列表
 */
export function fetchAssetTags(assetId: number, assetType: string) {
  return request<DataTag[]>({
    url: '/dataplatform/tag/asset',
    method: 'get',
    params: { assetId, assetType }
  });
}

/**
 * 自动打标签
 */
export function autoTag(assetId: number, assetType: string) {
  return request({
    url: '/dataplatform/tag/auto',
    method: 'post',
    params: { assetId, assetType }
  });
}

/**
 * 查询标签统计信息
 */
export function fetchTagStatistics() {
  return request<TagStatisticsDTO[]>({
    url: '/dataplatform/tag/statistics',
    method: 'get'
  });
}
